export function getDateType(type) {
  var now = new Date();
  var year = now.getFullYear(); //年
  var month =
    now.getMonth() < 9 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1; //月
  var day = now.getDate() < 10 ? "0" + now.getDate() : now.getDate(); //日

  var hh = now.getHours() < 10 ? "0" + now.getHours() : now.getHours(); //时
  var mm = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes(); //分
  var ss = now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds(); //秒

  switch (type) {
    case "y":
      return `${year}`;
    case "y-m":
      return `${year}-${month}`;
    case "y-m-d":
      return `${year}-${month}-${day}`;
    case "y-m-d h:m":
      return `${year}-${month}-${day} ${hh}:${mm}`;
    case "y-m-d h:m:s":
      return `${year}-${month}-${day} ${hh}:${mm}:${ss}`;
    default:
      return `${year}-${month}-${day} ${hh}:${mm}:${ss}`;
  }
}
//获取前num天日期；str：年月日以什么字符串分割
export function getDay(num, str) {
  let today = new Date();
  let nowTime = today.getTime();
  let ms = 24 * 3600 * 1000 * num;
  today.setTime(parseInt(nowTime + ms));
  let oYear = today.getFullYear();
  let oMoth = (today.getMonth() + 1).toString();
  if (oMoth.length <= 1) oMoth = "0" + oMoth;
  let oDay = today.getDate().toString();
  if (oDay.length <= 1) oDay = "0" + oDay;
  return oYear + str + oMoth + str + oDay;
}
//获取明天日期;str：年月日以什么字符串分割
export function yesterday(str) {
  let day = new Date();
  day.setTime(day.getTime() + 24 * 60 * 60 * 1000);
  return day.getFullYear() + str + (day.getMonth() + 1) + str + day.getDate();
}
//该使用场景为查询当前前一个月的信息，开始时间为上一个月零点零分零秒，结束时间为当前时间23点59分59秒
export function getLastMonth() {
  let agoDate = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000);
  agoDate.setHours(0);
  agoDate.setMinutes(0);
  agoDate.setSeconds(0);
  let now = new Date();
  now.setHours(23);
  now.setMinutes(59);
  now.setSeconds(59);
  return [chageDate(agoDate.getTime()), chageDate(now.getTime())];
}
//时间戳转换为时间
export function chageDate(timestamp) {
  // 时间戳：1637244864707
  /* 时间戳转换为时间 */
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + "-";
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  let h =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  let m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
    ":";
  let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}
//获取本月第一天
export function getFirstDay() {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = "01";
  m = m < 10 ? "0" + m : m; //月份补 0

  return [y, m, d].join("-");
}
//获取本月最后一天
export function getLastDay() {
  var y = new Date().getFullYear(); //获取年份
  var m = new Date().getMonth() + 1; //获取月份
  var d = new Date(y, m, 0).getDate(); //获取当月最后一日
  m = m < 10 ? "0" + m : m; //月份补 0
  d = d < 10 ? "0" + d : d; //日数补 0

  return [y, m, d].join("-");
}
//标准时间转换成yyyy-MM-dd
export function setForm(chinaStandard) {
  if (!chinaStandard) return ''
  var date = new Date(chinaStandard);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  var d = date.getDate();
  d = d < 10 ? "0" + d : d;
  var h = date.getHours();
  var minute = date.getMinutes();
  minute = minute < 10 ? "0" + minute : minute;
  let time = y + "-" + m + "-" + d; //这里如果不需要小时 分  后边的可以不需要拼接
  return time;
}
/**
* 获取几年前，默认一年前的今天
*/
export function getLastYear(yearNum = 1) {
  let today = new Date() //当天
  today.setFullYear(today.getFullYear() - yearNum)
  return today;
}
